using System;

namespace InseSoft.UIControllers
{
    public static class ReflectionHelper
    {
        public static object GetPropertyValue(object source, string propertyName)
        {
            var type = source.GetType();
            var property = type.GetProperty(propertyName);
            return property.GetValue(source, null);
        }

        public static void SetPropertyValue(object instance, string propertyName, object value)
        {
            var type = instance.GetType();
            var property = type.GetProperty(propertyName);

            if (property != null)
            {
                object convertedValue;
                if (value is DateTime && property.PropertyType == typeof(DateTime?))
                    convertedValue = value;
                else
                    convertedValue = Convert.ChangeType(value, property.PropertyType);

                property.SetValue(instance, convertedValue, null);
            }
        }

    }
}
